package com.example.enterprisemanager.mvvm.fragment.account.register;

import android.app.Application;
import android.arch.lifecycle.MutableLiveData;
import android.databinding.ObservableField;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.widget.Toast;

import com.example.enterprisemanager.R;
import com.example.enterprisemanager.base.BaseViewModel;
import com.example.enterprisemanager.binding.BindingCommand;
import com.example.enterprisemanager.mvvm.ben.User;
import com.example.enterprisemanager.util.ToastUtil;
import com.orhanobut.logger.Logger;

import java.util.Objects;

import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;

public class RegisterViewModel extends BaseViewModel<RegisterModel> {

    public BindingCommand registerCommand;

    public ObservableField<String> edtName;
    public ObservableField<String> edtPassword;
    public ObservableField<String> edtConfirm;

    public MutableLiveData<User> sendData;

    public RegisterViewModel(@NonNull Application application) {
        super(application, new RegisterModel());
        initCommand();
    }

    private void initCommand() {
        edtName = new ObservableField<>("");
        edtPassword = new ObservableField<>("");
        sendData = new MutableLiveData<>();
        edtConfirm = new ObservableField<>();

        registerCommand = new BindingCommand(() -> {
            Logger.d(edtName.get() + "registerCommand");

            if (edtName.get() == null || TextUtils.isEmpty(edtName.get())) {
                Toast.makeText(getApplication(), R.string.error_username, Toast.LENGTH_SHORT).show();
                return;
            }
            if (edtName.get().length() < 6) {
                Toast.makeText(getApplication(), R.string.str_userName_tip, Toast.LENGTH_SHORT).show();
                return;
            }
            if (edtPassword.get() == null || TextUtils.isEmpty(edtPassword.get()) || TextUtils.isEmpty(edtConfirm.get())) {
                Toast.makeText(getApplication(), R.string.error_pwd, Toast.LENGTH_SHORT).show();
                return;
            }
            if (edtPassword.get().length() < 6) {
                Toast.makeText(getApplication(), R.string.str_pass_tip, Toast.LENGTH_SHORT).show();
                return;
            }
            if (!TextUtils.equals(edtPassword.get(), edtConfirm.get())) {
                Toast.makeText(getApplication(), R.string.str_pass_confirm, Toast.LENGTH_SHORT).show();
                return;
            }
            User user = new User();
            user.setUsername(edtName.get());
            user.setPassword(edtPassword.get());
            user.setAge(18);
            user.setGender(0);
            user.setPower(5);

            mModel.signUp(user, new Consumer<User>() {
                @Override
                public void accept(User user) throws Exception {
                    ToastUtil.show(getApplication(), "注册成功");
                    sendData.postValue(user);
                }
            }, null);

        });
    }

}
